package com.ruoyi.patrol.service.impl;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.patrol.domain.PtLine;
import com.ruoyi.patrol.domain.PtLineGuard;
import com.ruoyi.patrol.domain.PtLineTask;
import com.ruoyi.patrol.domain.vo.ScheduleInfo;
import com.ruoyi.patrol.mapper.PtLineGuardMapper;
import com.ruoyi.patrol.mapper.PtLineRecordDetailMapper;
import com.ruoyi.patrol.service.IPtLineGuardService;
import com.ruoyi.patrol.service.IPtLineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;

/**
 * 巡查线路与安保人员关联Service业务层处理
 *
 * @author lkm
 * @date 2022-11-12
 */
@Service
public class PtLineGuardServiceImpl implements IPtLineGuardService {
    @Autowired
    private PtLineGuardMapper ptLineGuardMapper;
    @Autowired
    private IPtLineService ptLineService;
    @Autowired
    private PtLineRecordDetailMapper ptLineRecordDetailMapper;
    /**
     * 查询巡查线路与安保人员关联
     *
     * @param id 巡查线路与安保人员关联ID
     * @return 巡查线路与安保人员关联
     */
    @Override
    public PtLineGuard selectPtLineGuardById(Long id) {
        PtLineGuard ptLineGuard = ptLineGuardMapper.selectPtLineGuardById(id);
        PtLineTask ptLineTask = new PtLineTask();
        ptLineTask.setLineId(ptLineGuard.getLineId());
        ptLineTask.setGroupId(ptLineGuard.getGroupId());
        return ptLineGuard;
    }

    @Override
    public List<ScheduleInfo> listUndone(ScheduleInfo scheduleInfo) {
        return ptLineGuardMapper.listUndone(scheduleInfo);
    }

    /**
     * 查询巡查线路与安保人员关联列表
     *
     * @param ptLineGuard 巡查线路与安保人员关联
     * @return 巡查线路与安保人员关联
     */
    @Override
    @DataScope(deptAlias = "l")
    public List<PtLineGuard> selectPtLineGuardList(PtLineGuard ptLineGuard) {
        return ptLineGuardMapper.selectPtLineGuardList(ptLineGuard);
    }

    @Override
    public List<PtLineGuard> selectPtLineGuardListdp(PtLineGuard ptLineGuard) {
        return ptLineGuardMapper.selectPtLineGuardListdp(ptLineGuard);
    }

    /**
     * 新增巡查线路与安保人员关联
     *
     * @param ptLineGuard 巡查线路与安保人员关联
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertPtLineGuard(PtLineGuard ptLineGuard) {
        // 同步巡查线路的巡逻点位个数
        PtLine line = ptLineService.selectPtLineById(ptLineGuard.getLineId());
        ptLineGuard.setSiteCount(line.getSiteCount());
        ptLineGuard.setCreateTime(DateUtils.getNowDate());

        int result = 0;
        for (String date : ptLineGuard.getDates()) {  // 多个日期
            ptLineGuard.setDate(DateUtils.parseDate(date));
            result = ptLineGuardMapper.insertPtLineGuard(ptLineGuard);
            HashMap<String, Object> parameter = new HashMap<>();
            parameter.put("id", ptLineGuard.getId());
            parameter.put("radius", "clocking_radius");
            ptLineRecordDetailMapper.insertPtSiteRecordGuards(parameter);
        }

        return result;
    }

    /**
     * 修改巡查线路与安保人员关联
     *
     * @param ptLineGuard 巡查线路与安保人员关联
     * @return 结果
     */
    @Override
    public int updatePtLineGuard(PtLineGuard ptLineGuard) {
        ptLineGuard.setUpdateTime(DateUtils.getNowDate());
        return ptLineGuardMapper.updatePtLineGuard(ptLineGuard);
    }

    /**
     * 批量删除巡查线路与安保人员关联
     *
     * @param ids 需要删除的巡查线路与安保人员关联ID
     * @return 结果
     */
    @Override
    public int deletePtLineGuardByIds(Long[] ids) {
        return ptLineGuardMapper.deletePtLineGuardByIds(ids);
    }

    /**
     * 删除巡查线路与安保人员关联信息
     *
     * @param id 巡查线路与安保人员关联ID
     * @return 结果
     */
    @Override
    public int deletePtLineGuardById(Long id) {
        return ptLineGuardMapper.deletePtLineGuardById(id);
    }


    @Override
    public long selectCount(PtLineGuard ptLineGuard) {
        return ptLineGuardMapper.selectCount(ptLineGuard);
    }
}
